package com.example.demo;

import com.example.demo.Component.UserComponent;
import com.example.demo.UserInfo.UserInfo;
import com.example.demo.controller.UserController;
import com.example.demo.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;

/**
 * 通过五大注解将对象交给Spring 处理,Spring会创建对象,管理对象
 * 单例,可修改
 *
 */
@SpringBootApplication
@ComponentScan("com.example.demo")//指定扫描路径
//默认是启动类所在的路径
public class SpringIocDemoApplication {

	public static void main(String[] args) {
		ApplicationContext context =SpringApplication.run(SpringIocDemoApplication.class, args);
//		//根据类型从Spring中获取对象
//		UserController controller = context.getBean(UserController.class);
//		controller.sayHi();
//		//根据名称获取
//		UserService userService =(UserService) context.getBean("userService");
//		userService.sayHi();
//		//双重
//		UserComponent component = context.getBean("userComponent", UserComponent.class);
//		component.sayHi();
//		UserInfo userInfo = (UserInfo) context.getBean("u1");
//		System.out.println(userInfo);
//		UserInfo userInfo2 =(UserInfo) context.getBean("u2");
//		System.out.println(userInfo2);
//		UserComponent userComponent = (UserComponent) context.getBean("userComponent");
//		userComponent.sayHi();
		UserService userService =(UserService) context.getBean("userService");
		userService.sayHi();
	}

}
